package com.iwhalecloud.citybrain.flow.platform.manager.domain.model.factory;

import com.iwhalecloud.citybrain.flow.platform.manager.domain.enums.MultiInstanceKeyEnum;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.enums.TaskCompletedKeyEnum;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.CompletionCondition;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.ModelCustomConfigDomain;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.repository.FlowReModelCustomConfigRepository;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author liyang
 * @version 1.0
 * @date 2021-07-23 15:15:20
 **/
@Service
public class NodeCompletionConditionFactory implements ApplicationContextAware {


    static FlowReModelCustomConfigRepository flowReModelCustomConfigRepository;

    public List<CompletionCondition> getCompletionConditons(String depolyId,String nodeCode){
        List<ModelCustomConfigDomain>  modelCustomConfigDomainList = flowReModelCustomConfigRepository.listModelConfigByDeployIdAndType(depolyId,
                nodeCode, MultiInstanceKeyEnum.COMPLETION_CONDITION.getCode());
        List<CompletionCondition> completionConditionList = null;
        if (Objects.nonNull(modelCustomConfigDomainList) && modelCustomConfigDomainList.size() > 0){
            completionConditionList = new ArrayList<>(modelCustomConfigDomainList.size());
            for (ModelCustomConfigDomain modelCustomConfigDomain : modelCustomConfigDomainList) {
                CompletionCondition completionCondition = new CompletionCondition();
                completionCondition.setDeploymentId(modelCustomConfigDomain.getDeploymentId());
                completionCondition.setValue(modelCustomConfigDomain.getConfig());
                completionCondition.setNodeCode(modelCustomConfigDomain.getNodeCode());
                completionConditionList.add(completionCondition);
            }
        }
        return completionConditionList;
    }


    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        flowReModelCustomConfigRepository = applicationContext.getBean(FlowReModelCustomConfigRepository.class);
    }
}
